/**
 * Created by Administrator on 2018/4/10.
 * 有三级医院多个子医院的时候，设置hospLevel和hospCode, 如果一级医院选择了，则设置hospLevel = 1,
 * 二级医院大标题选择后，二级子医院全部选择，如果大标题未选择，子医院选择若干，则选择子医院code
 */
export function setHospLevelAndHospCode(filterN, filter) {
  let hospLevel;
  let hospCode;
  // 用户结论的判定
  let resultLevel = [];
  let resultId = [];
// 判断是否有一级医院，如果有则把hospLevel设置为1，没有则设置为空
  let indexOne = filter.indexOf('1')
  if (indexOne !== -1) {
    hospLevel = '1'
    filter.splice(indexOne, 1)
    resultLevel.push('1')
  } else {
    hospLevel = ''
  }
  // 如果二级医院或者三级医院勾选，则全部把子医院的ID传入hospCode
  let hosDetail = filterN.nc.detailHospital
  console.log('hosDetail ..' + JSON.stringify(hosDetail))
  let indexTwo = filter.indexOf('2')
  if (indexTwo !== -1) {
    console.log('选择了二级医院')
    filter.splice(indexTwo, 1)
    let twoHosChild = hosDetail.find((child) => {
      return child.selectId == '2';
    })
    if (twoHosChild) {
      console.log('选择了二级医院: ' + JSON.stringify(twoHosChild))
      twoHosChild.child.forEach((child) => {
        if (filter.indexOf(child.selectId) === -1) {
          filter.push(child.selectId)
        }
      })
    }
    resultLevel.push('2')
  } else {
    let twoHosChild = hosDetail.find((child) => {
      return child.selectId == '2';
    })
    if (twoHosChild) {
      console.log('选择了二级医院: ' + JSON.stringify(twoHosChild))
      twoHosChild.child.forEach((child) => {
        if (child.checked) {
          resultId.push(child.selectId)
        }
      })
    }
  }
  let indexThree = filter.indexOf('3')
  if (indexThree !== -1) {
    console.log('选择了三级医院')
    filter.splice(indexThree, 1)
    let threeHosChild = hosDetail.find((child) => {
      return child.selectId == '3';
    })
    if (threeHosChild) {
      console.log('选择了三级医院: ' + JSON.stringify(threeHosChild))
      threeHosChild.child.forEach((child) => {
        if (filter.indexOf(child.selectId) === -1) {
          filter.push(child.selectId)
        }
      })
    }
    resultLevel.push('3')
  } else {
    let threeHosChild = hosDetail.find((child) => {
      return child.selectId == '3';
    })
    if (threeHosChild) {
      console.log('选择了三级医院: ' + JSON.stringify(threeHosChild))
      threeHosChild.child.forEach((child) => {
        if (child.checked) {
          resultId.push(child.selectId)
        }
      })
    }
  }
  hospCode = filter.join(',')

  resultLevel = resultLevel.join(',')
  resultId = resultId.join(',')
  return {
    hospLevel,
    hospCode,
    resultLevel,
    resultId
  }
}
/**
 * 获取默认年份
 * @param filter
 * @returns {*}
 */
export function getDefaultYear(filter) {
  return filter.years[0]
}

export function getDefaultSiYearInterval() {
  // 获取当前年份和月份
  let myDate = new Date();
  let curYear = myDate.getFullYear();
  let curMonth = myDate.getMonth() + 1;
  let curSiYear;
  if (curMonth >= 7) {
    curSiYear = curYear - 1
  } else {
    curSiYear = curYear - 2
  }
  return curSiYear
}
/**
 * 获取默认统筹区
 * @param filter
 * @returns {*}
 */
export function getDefaultAreaCode(filter) {
  let areaCode = []
  filter.nc.planArea.map((item, index) => {
    if (index > 0 && index <= 6) {
      areaCode.push(item.selectId)
    }
  })
  return areaCode
}
/**
 * 获取默认医院等级（复选框）
 * @param filter
 * @returns {*}
 */
export function getDefaultHospLevel(filter) {
  return filter.nc.hospitalRankType[1].selectId
}
/**
 * 获取医疗类别的默认值（不带有全部的取值第一项）
 * @param filter
 * @returns {*}
 */
export function getDefaultInsuredTypeWithoutAll(filter) {
  return filter.nc.insuredTypeSecond[0].selectId
}

/**
 * 获取医疗类别的默认值（不带有全部的取值第一项）
 * @param filter
 * @returns {*}
 */
export function getDefaultFreeSortTypeWithoutAll(filter) {
  return filter.nc.freeSortType[0].selectId
}

/**
 * 获取默认典型单病种的ID
 * @param filter
 * @returns {*}
 */
export function getDefaultSingleEntityType(filter) {
  let singleEntityType = []
  filter.nc.singleEntityType.map((item, index) => {
    if (index >= 0 && index <= 4) {
      singleEntityType.push(item.selectId)
    }
  })
  return singleEntityType
}

/**
 * 获取默认三级医院的全部子医院
 * @param filter
 * @returns {*}
 */
export function getDefaultThreeHospChildId(filter) {
  let hospId = []
  filter.nc.detailHospital.map((item, index) => {
    if (item.selectId == 3) {
      if (item.child) {
        item.child.forEach(child => {
          hospId.push(child.selectId)
        })
      }
    }

  })
  return hospId
}

